//
//  MyAltasViewController.m
//  jingdongfang
//
//  Created by MTiMac02 on 2017/11/13.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "MyAltasViewController.h"
#import "AlbumViewCell.h"
#import "photoAlbumViewController.h"
#import "NewConstructAlbumViewController.h"
#import "IGGetAtlasApi.h"
#import "IGAtlasListModel.h"
#import "IGAddAtlasDetailApi.h"
#import "IGDeleteAtlasApi.h"

@interface MyAltasViewController ()<UITableViewDataSource, UITableViewDelegate>{
    UIView *nonView;
}
@property(nonatomic,strong)IGGetAtlasApi *getAtlasApi;
@property(nonatomic,strong)IGDeleteAtlasApi *deleteAtlasApi;
@property (nonatomic, strong)NSMutableArray *dataArr;
@end

@implementation MyAltasViewController

#pragma mark -生命周期
- (void)viewDidLoad
{
    [super viewDidLoad];
//    self.navigationItem.title=@"我的图册";
    self.navigationItem.title= NSLocalizedString(@"My Album", nil);
    self.table.backgroundColor =[UIColor mainBackgroundColor];
//    [self ig_setupRightButtonItemWithTitle:@"新建" image:nil highLightedImage:nil];
    [self ig_setupRightButtonItemWithTitle:NSLocalizedString(@"New", nil) image:nil highLightedImage:nil];
    nonView=[[UIView alloc]initWithFrame:CGRectMake(0, 40, KSCREENWIDTH, 100)];
    nonView.backgroundColor=[UIColor clearColor];
    UILabel *nonLable=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 60)];
    nonLable.center=nonView.center;
//    nonLable.text=@"您的图册空空如也~";
    nonLable.text=NSLocalizedString(@"Empty Album", nil);
    nonLable.numberOfLines=1;
    nonLable.textAlignment=NSTextAlignmentCenter;
    nonLable.font=[UIFont font16];
    nonLable.textColor=[UIColor blackColor];
    [nonView addSubview:nonLable];
    nonView.hidden=YES;
    [self.table addSubview:nonView];
    [self.table registerNib:[UINib nibWithNibName:@"AlbumViewCell" bundle:nil] forCellReuseIdentifier:@"AlbumViewCell"];
    self.dataArr = [@[] mutableCopy];
    [self loadAtlasInfo];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self loadAtlasInfo];
}

-(void)loadAtlasInfo
{
    @KWeakObj(self)
    self.getAtlasApi = [IGGetAtlasApi new];
    self.getAtlasApi.worksId = _workId;
    [self.getAtlasApi startWithDecodeClass:[IGGetAtlasResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error)
     {
         [selfWeak.table ig_endHeaderRefreshing];
         [selfWeak.table ig_endFooterRefreshing];
         if (responseObject)
         {
             IGGetAtlasResp *resp=responseObject;
             IGGetAtlasModel *model=resp.data;
             if (model.atlasList&&model.atlasList.count>0)
             {
                 nonView.hidden=YES;
                 selfWeak.dataArr=[NSMutableArray arrayWithArray:model.atlasList];
                 
             }
             if (model.atlasList.count==0) {
                 selfWeak.dataArr=[NSMutableArray arrayWithArray:model.atlasList];
                 nonView.hidden=NO;
             }
             [selfWeak.table reloadData];
         }
     }];
}
#pragma mark -点击导航栏右侧按钮
- (void)ig_rightButtonPressed:(id)sender
{
    [UIViewController ig_pushViewControllerClass:[NewConstructAlbumViewController class] animated:YES];
}

#pragma mark -Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArr.count > 0 ? self.dataArr.count : 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AlbumViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"AlbumViewCell" forIndexPath:indexPath];
    if (cell==nil)
    {
        cell = [[AlbumViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AlbumViewCell"];
    }
    IGAtlasListModel *model=_dataArr[indexPath.section];
    cell.atlasListmodel = model;
    cell.worksId = _workId;
    __weak typeof(self) weakSelf = self;
    cell.deleteCell  = ^{
        weakSelf.deleteAtlasApi=[[IGDeleteAtlasApi alloc]initWithUid:[IGUser getUid] atlasId:model.aID];
        [weakSelf.deleteAtlasApi startWithDecodeClass:[IGDeleteAtlasResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
            if (responseObject)
            {
                [weakSelf loadAtlasInfo];
            }
        }];
    };
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  100;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return 10.f;
    }
    return 0.001f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    IGAtlasListModel *list=_dataArr[indexPath.section];
    if (_workId.length > 0)
    {
        if ([list.ifExist isEqualToString:@"0"])
        {
            [self addPictureWithId:list.aID];
        }
    }
    else
    {
        photoAlbumViewController *vc = [[photoAlbumViewController alloc] init];
        vc.aId=list.aID;
        vc.albumNam=list.title;
        [UIViewController ig_pushViewController:vc animated:YES];
    }
}

- (void)addPictureWithId:(NSString *)aId
{
    IGAddAtlasDetailApi *api = [IGAddAtlasDetailApi new];
    api.aId = aId;
    api.pids = _workId;
    [api startWithDecodeClass:[IGAddAtlasDetailResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject)
        {
            [self.navigationController popViewControllerAnimated:YES];
//            [self presentMessageTips:@"添加成功"];
            [self presentMessageTips:NSLocalizedString(@"Added Successfully", nil)];
        }
    }];
    
}
- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
//    return [[NSAttributedString alloc] initWithString:@"您还没有收藏任何作品~" attributes:nil];
    return [[NSAttributedString alloc] initWithString:NSLocalizedString(@"No Collection", nil) attributes:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
